package com.promfy.net.apn;

import com.promfy.R;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.Bundle;

public class ApnSettingActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        initApn(this);
//        try {
//			insertApn("ggg", "460", "01", "3gnet", "", "", "", "", "80", "", "", "", -1, "default,dun", 1);
////			insertApn(name, numeric, mcc, mnc, apn, user, server, password, proxy, port, mmsproxy, mmsport, mmsc, authtype, type, current)
//		} catch (Exception e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
 
//        }else{
//        	ContentResolver resolver = getContentResolver();
//        	ContentValues values = new ContentValues();
//        	values.put("apn", "3gnet");
//        	resolver.update(Uri.parse("content://telephony/carriers"), values, "name=?", new String[]{"ccc"});
//        }
    }
    
    
    
    static boolean isCMWap(Context context){
        ConnectivityManager conManager = (ConnectivityManager)context.
                                         getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo ni = conManager.getActiveNetworkInfo();
        if(ni != null){
                  return (ni.getExtraInfo().equals("cmwap")? true : false);
        }
        return true; 
    }
     
    public void initApn(Context context){
    	//0. set sp.
//    	SharedPreferences  sp1 = getSharedPreferences("apn",  
//  				Context.MODE_WORLD_WRITEABLE);
//  		Editor edit = sp1.edit();
//  		edit.putString("name", "aaa");
//  		edit.putString("mcc", "460");
//  		edit.putString("mnc", "01");
//  		edit.putString("apn", "3gnet"); 
//  		edit.putString("user", ""); 
//  		edit.putString("server", ""); 
//  		edit.putString("password", "");
//  		edit.putString("proxy", "");
//  		edit.putString("port", "80");
//  		edit.putString("mmsproxy", "");
//  		edit.putString("mmsport", "");
//  		edit.putString("mmsc", "");
//  		edit.putInt("authtype", -1);  
//  		edit.putString("type", "default,dun");
//  		edit.putInt("current", 1);
//  		edit.commit(); 
    	
    	//1. read from sp.
    	SharedPreferences sp = context.getSharedPreferences("apn", Context.MODE_WORLD_WRITEABLE);
//    	sp.edit().putString("name", "123");
//    	sp.edit().commit();

    	if(sp == null) return; 
    	
    	//2. update current apn
    	ContentResolver resolver = getContentResolver();
    	resolver.delete(Uri.parse("content://telephony/carriers"), "current=?", new String[]{"1"});
    	//3. set new apn
		try {
//			insertApn("mmm", "460", "01", "3gnet", "", "", "", "", "80", "", "", "", -1, "default,dun", 1);
			insertApn(sp.getString("name", ""),
					sp.getString("mcc", ""),
					sp.getString("mnc", ""),
					sp.getString("apn", ""),
					sp.getString("user", ""), 
					sp.getString("server", ""),
					sp.getString("password", ""),
					sp.getString("proxy", ""),
					sp.getString("port", ""),
					sp.getString("mmsproxy", ""),
					sp.getString("mmsport", ""),
					sp.getString("mmsc", ""),
					sp.getInt("authtype", -1),
					sp.getString("type", ""),
					sp.getInt("current", 1));
		} catch (Exception e) { 
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }  
    
    /**
     * insert data in telephony.db. system will fresh apn selection.
     * if some params don't need to set, set it "", do not set it null.
     * @param name
     * @param numeric
     * @param mcc
     * @param mnc
     * @param apn
     * @param user
     * @param server
     * @param password
     * @param proxy
     * @param port
     * @param mmsproxy
     * @param mmsport
     * @param mmsc
     * @param authtype
     * @param type
     * @param current
     * @throws Exception
     */
    public void insertApn(String name,
    		String mcc,
    		String mnc,
    		String apn,
    		String user,
    		String server,
    		String password,
    		String proxy,
    		String port,
    		String mmsproxy,
    		String mmsport,
    		String mmsc,
    		Integer authtype,
    		String type,
    		Integer current) throws Exception{
    	ContentResolver resolver = getContentResolver();
		ContentValues values = new ContentValues();
		values.put("name", name);
		values.put("numeric", mcc + mnc);
		values.put("mcc", mcc);
		values.put("mnc", mnc);
		values.put("apn", apn);
		values.put("user", user);
		values.put("server", server);
		values.put("password", password);
		values.put("proxy", proxy);
		values.put("port", port);
		values.put("mmsproxy", mmsproxy);
		values.put("mmsport", mmsport);
		values.put("mmsc", mmsc);
		values.put("authtype", authtype);
		values.put("type", type);
		values.put("current", current);
		resolver.insert(Uri.parse("content://telephony/carriers"), values);
    }
}